package com.darrenchan.spark

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Spark Streaming处理Socket数据
  */
object NetworkWordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
    /**
      * 创建StreamingContext需要两个参数：SparkConf和batch interval
      */
    val ssc = new StreamingContext(sparkConf, Seconds(20))
    ssc.addStreamingListener(new BjhListener("NetworkWordCount", 20))

    val lines = ssc.socketTextStream("localhost", 9999)

//    val result = lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_)
    val result = lines.map(_.split("_")(1))

    result.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
